import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui';
import local from "@/utils/local.js";

Vue.use(VueRouter)

const normalRoutes = [
  // 访问页面时自动跳转登录页
  {
    path: '/', component: () => import("@/views/login/Login.vue"), redirect: "/login", children: [
      // 登录页
      { path: "/login", component: () => import("@/views/login/Login.vue") },
    ]
  },
  // 后台首页
  {
    path: "/index", icon: "el-icon-discount", isMenu: true, meta: { title: "后台首页" }, component: () => import("@/views/index/Layout.vue"), children: [
      { path: "", component: () => import("@/views/index/Index.vue") },
    ]
  },
  // 订单管理
  {
    path: "/order", icon: "el-icon-s-order", isMenu: true, meta: { title: "订单管理" }, component: () => import("@/views/index/Layout.vue"), children: [
      { path: "", component: () => import("@/views/order/Order.vue") },
    ]
  },
  // 商品管理
  {
    path: "/goods", icon: "el-icon-shopping-bag-1", isMenu: true, meta: { title: "商品管理" }, component: () => import("@/views/index/Layout.vue"), redirect: "/goods/list", children: [
      { path: "/goods/list", isMenu: true, meta: { title: "商品列表" }, component: () => import("@/views/goods/GoodsList.vue") },
      { path: "/goods/add", isMenu: true, meta: { title: "添加商品" }, component: () => import("@/views/goods/GoodsAdd.vue") },
      { path: "/goods/category", isMenu: true, meta: { title: "商品分类" }, component: () => import("@/views/goods/GoodsCategory.vue") },
      { path: "/goods/edit", meta: { title: "修改商品" }, component: () => import("@/views/goods/GoodsEdit.vue") },
    ]
  },
]

const superRoutes = [
  // 店铺管理
  {
    path: "/shop", icon: "el-icon-s-shop", isMenu: true, meta: { title: "店铺管理" }, component: () => import("@/views/index/Layout.vue"), children: [
      { path: "", component: () => import("@/views/shop/Shop.vue") },
    ]
  },
  // 账号管理
  {
    path: "/account", icon: "el-icon-s-custom", isMenu: true, meta: { title: "账号管理" }, component: () => import("@/views/index/Layout.vue"), redirect: "/account/list", children: [
      { path: "/account/list", isMenu: true, meta: { title: "账号列表" }, component: () => import("@/views/account/AccountList.vue") },
      { path: "/account/add", isMenu: true, meta: { title: "添加账号" }, component: () => import("@/views/account/AccountAdd.vue") },
      { path: "/account/changepassword", isMenu: true, meta: { title: "修改密码" }, component: () => import("@/views/account/ChangePassword.vue") },
      { path: "/account/personal", meta: { title: "管理员信息" }, component: () => import("@/views/account/Personal.vue") },
    ]
  },
  // 销售统计
  {
    path: "/count", icon: "el-icon-time", isMenu: true, meta: { title: "销售统计" }, component: () => import("@/views/index/Layout.vue"), redirect: "/count/goods", children: [
      { path: "/count/goods", isMenu: true, meta: { title: "商品统计" }, component: () => import("@/views/count/CountGoods.vue") },
      { path: "/count/order", isMenu: true, meta: { title: "订单统计" }, component: () => import("@/views/count/CountOrder.vue") },
    ]
  },
]

// 拆分路由，将路由拆分为不同的角色
export function showRouter() {
  let role = local.get("role");
  if (role === "super") {
    return [...normalRoutes, ...superRoutes];
  } else {
    return normalRoutes;
  }
}

const router = new VueRouter({
  routes: showRouter(),  //根据不同的条件生成不同的路由
})

export default router

// 重复点击页面跳转时报错
// 获取原型对象push函数
const originalPush = VueRouter.prototype.push
// 修改原型对象中的push函数
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由守卫
router.beforeEach(function (to, from, next) {
  if (to.path === "/login") {
    next();  //登录页面直接放行
  } else {
    let token = local.get("token");
    if (token) {
      next();  //存在token直接放行
    } else {
      Message.error("你是哪个，不登录还想访问页面！");
      next({ path: "/login" });
    }
  }
})